#!/usr/bin/env python 3.6
# -*- coding: utf-8 -*-
# @Time      :2019/2/13
# @Author    :Tian Maohong
# @Email     :tianmaohong@bbdservice.com
# @File      :R37_EC_UpaidExternalGuaranteeBadAmtCheck
# @Software  :PyCharm


'''eof
name:申请企业未结清对外担保业务余额校验（不良类）
code:R37_EC_UpaidExternalGuaranteeBadAmtCheck
tableName:
columnName:
groups:企业征信校验
dependencies:EC_DSJ
type:常用指标
datasourceType:在线指标
description:
eof'''



import sys


reload(sys)
sys.setdefaultencoding('utf-8')



null_type_list = ['', ' ', None, 'null', 'Null', 'NULL', '/', '-', '--']

def getTable(table_Name):
    try:
        data = EC_DSJ['data'].get(table_Name)
        if data in null_type_list:
            return None
        else:
            # 存在1条为空的数据[{}]
            if isinstance(data, list) and len(data) == 1 and data[0]['ReportNo'] in null_type_list:
                return [{}]
            # 存在空list []
            elif isinstance(data, list) and len(data) == 0:
                return None
            else:
                return data
    except:
        return None


def isfloat(x):
    try:
        if isinstance(float(x), float) and float(x)>=0:
            if str(x) in ['inf', 'infinity', 'INF', 'INFINITY', 'True']:
                return False
            else:
                return True
        else:
            return False
    except:
        return False



def upaidExternalGuaranteeBadAmtCheck():
    """
    Factor=对外担保信息概要（ECRSummaryGuarantee）中的"被担保业务余额(不良)"的值
    :return:
    """

    ECRSummaryGuarantee = getTable('ECRSummaryGuarantee')

    if ECRSummaryGuarantee is None:
        return u'缺失值'
    elif ECRSummaryGuarantee == [{}]:
        return 0
    else:
        GuaranteedBalance_Bad = ECRSummaryGuarantee[0].get('GuaranteedBalance_Bad')
        if isfloat(GuaranteedBalance_Bad):
            return round(float(GuaranteedBalance_Bad), 4)
        else:
            return u'缺失值'



result = upaidExternalGuaranteeBadAmtCheck()